<template>
 <div class="container">  
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" style="width:100%" class="demo-ruleForm">
        <el-form-item label="姓名：" prop="name" style="width:40%">
            <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="职务：" prop="roleIds">
          <el-checkbox-group v-model="ruleForm.roleIds" prop="roleIds">
            <el-checkbox label="2">班主任(助教)</el-checkbox>
            <el-checkbox label="3">授课老师</el-checkbox>
            <el-checkbox label="4">课程顾问</el-checkbox>
            <el-checkbox label="1">其他</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="手机：" prop="phone" style="width:40%">
            <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="设置登录密码：" prop="password">
            <el-input v-model="ruleForm.password" type="password" id="Password" style="width:35%"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        </el-form-item>
    </el-form>
 </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: "",
        phone: "",
        radio: "",
        password: "",
        roleIds:[],
      },  
      rules: {
        name: [{ required: true, message: "请输入名字", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { pattern: /^1[34578]\d{9}$/, message: "请输入正确的手机号" }
        ], 
        roleIds: [
          {required:true,message:"请选择对应职务"}
        ], 
        password: [{required:true, message:"请输入密码", trigger: "blur"}]
      }
    };
  },
  methods: {
    submitForm(formName) {
      var that = this;
      let organizationId = JSON.parse(localStorage.getItem("organizationIds"))[0];
      this.url = "/api/user/xzs/organizationUser.save";
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios({
            url: this.url,
            method: "post",
            data: {
              phone: that.ruleForm.phone,
              name: that.ruleForm.name,
              password: that.ruleForm.password,
              roleIds:that.ruleForm.roleIds,
              organizationId: organizationId,
            }
          }).then(res => {
            console.log(res.data);
              if(res.data.errorCode === "0"){
                this.$message({
                  showClose: true,
                  message: "添加成功",
                  type: "success"
                });
                this.$router.push("/Personnel");
                location.reload(0)
              }else if(res.data.errorCode === "400005"){
                this.$message({
                  showClose: true,
                  message: "手机号已存在，请跟换号码",
                  type: "warning"
                });
              }
            }).catch(err => {
              console.log(err);
            });
          } else {
            return false;
          }
      });
    }
  },
  mounted(){
  }
};
</script>

<style>

</style>
